package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.*;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.SmsWnsSchoolFeesDelayService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 学费缓交表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-01-30
 */
@RestController
@RequestMapping("/welcomeNewStudent/schoolFeesDelay")
@SecretBody
public class SmsWnsSchoolFeesDelayController {


    @Resource
    private SmsWnsSchoolFeesDelayService smsWnsSchoolFeesDelayService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation("提交申请先查询是否设置了辅导员和院负责人")
    @GetMapping("/assistantAndOrgLeader")
    public ResponseResult getAssistantAndOrgLeader(HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsSchoolFeesDelayService.getAssistantAndOrgLeader(userId);
    }

    @ApiOperation("提交学费缓交申请")
    @PostMapping("/add")
    public ResponseResult addSchoolFeesDelay(@RequestBody SmsWnsSchoolFeesDelayReq schoolFeesDelayReq, HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsSchoolFeesDelayService.addSchoolFeesDelay(schoolFeesDelayReq,userId);
    }

    @ApiOperation("撤销申请")
    @PostMapping("/cancel")
    public ResponseResult cancelSchoolFeesDelay(@RequestBody SmsWnsSchoolFeesDelayCancelReq cancelReq, HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsSchoolFeesDelayService.cancelSchoolFeesDelay(cancelReq,userId);
    }
    @ApiOperation("审批")
    @PostMapping("/allow")
    public ResponseResult allowSchoolFeesDelay(@RequestBody SmsWnsSchoolFeesDelayAllowReq allowReq, HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsSchoolFeesDelayService.allowSchoolFeesDelay(allowReq,userId);
    }

    @ApiOperation("审批列表")
    @GetMapping("/list")
    public ResponseResult getSchoolFeesDelayList(@RequestParam(value = "searchStr", required = false) String searchStr,
                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "applyType") Integer applyType,
                                                 @RequestParam(value = "sort",required = false) Integer sort,
                                                 HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsSchoolFeesDelayService.getSchoolFeesDelayList(searchStr,applyType,sort,userId,pageSize,pageNum);
    }

    @ApiOperation("审批详情")
    @GetMapping("/detail")
    public ResponseResult getSchoolFeesDelayDetail(@RequestParam(value = "applyId") String applyId, HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsSchoolFeesDelayService.getSchoolFeesDelayDetail(applyId,userId);
    }

    @ApiOperation("申请人指定流程（申请前获取）")
    @GetMapping("/processList")
    public ResponseResult getSchoolFeesDelayOriginSettingProcess(HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsSchoolFeesDelayService.getSchoolFeesDelayOriginSettingProcess(userId);
    }

    @ApiOperation(value = "查询所有申请记录")
    @GetMapping("/selectAll")
    public ResponseResult selectAllSchoolFeesDelay(@RequestParam(value = "stuName", required = false) String stuName,
                                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "collegeId",required = false) String collegeId,
                                                   @RequestParam(value = "majorId", required = false) String majorId,
                                                   @RequestParam(value = "deliveryDate", required = false) String deliveryDate,
                                                   @RequestParam(value = "stuIdCard", required = false) String stuIdCard,
                                                   @RequestParam(value = "allowStatus", required = false) Integer allowStatus) {
        return smsWnsSchoolFeesDelayService.selectAllSchoolFeesDelay(deliveryDate, stuName, allowStatus, collegeId, majorId, stuIdCard, pageSize, pageNum);
    }

    @ApiOperation(value = "查询自己所有申请记录")
    @GetMapping("/selectStuAll")
    public ResponseResult selectStuAllSchoolFeesDelay(@RequestParam(value = "deliveryDate", required = false) String deliveryDate,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "allowStatus", required = false) Integer allowStatus,
                                                      HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsSchoolFeesDelayService.selectStuAllSchoolFeesDelay(deliveryDate,allowStatus,userId,pageSize, pageNum);
    }

    @ApiOperation(value = "文件下载")
    @GetMapping("/download")
    public ResponseResult tableDownload(@RequestParam(value = "applyId") String applyId, HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsWnsSchoolFeesDelayService.tableDownload(applyId,response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;

    }

    @ApiOperation("批量导出")
    @PostMapping("/batch/export")
    public ResponseResult exportSchoolFeesDelay(@RequestBody SmsWnsSchoolFeesDelayExportReq schoolFeesDelayExportReq, HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsWnsSchoolFeesDelayService.exportSchoolFeesDelay(schoolFeesDelayExportReq, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiOperation(value = "强制归档")
    @PostMapping("/forced")
    public ResponseResult forcedFinishSchoolFeesDelay(@RequestBody SmsWnsSchoolFeesDelayForcedReq forcedReq, HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsSchoolFeesDelayService.forcedFinishSchoolFeesDelay(forcedReq.getApplyId(),userId);
    }

    @ApiOperation(value = "强制流转")
    @PostMapping("/forcedTorsion")
    public ResponseResult forcedTorsionSchoolFeesDelay(@RequestBody SmsWnsSchoolFeesDelayDetailTorsionReq torsionReq){
        return smsWnsSchoolFeesDelayService.forcedTorsionSchoolFeesDelay(torsionReq);
    }

    @ApiOperation(value = "代审批")
    @PostMapping("/replaceApply")
    public ResponseResult replaceApplySchoolFeesDelay(HttpServletRequest request, @RequestBody SmsWnsSchoolFeesDelayAllowReq allowReq){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsSchoolFeesDelayService.replaceApplySchoolFeesDelay(userId,allowReq);
    }

    @ApiOperation(value = "强制驳回")
    @PostMapping("/forcedReject")
    public ResponseResult forcedRejectSchoolFeesDelay(@RequestBody SmsWnsSchoolFeesDelayForcedReq forcedReq,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsSchoolFeesDelayService.forcedRejectSchoolFeesDelay(forcedReq,userId);
    }

    @ApiOperation(value = "删除学费缓交")
    @PostMapping("/deleted")
    public ResponseResult deletedSchoolFeesDelay(@RequestParam("applyId") String applyId){
        return smsWnsSchoolFeesDelayService.deletedSchoolFeesDelay(applyId);
    }

}

